<?php
class fileReader {
//var $document;
//var $tipo;

public function read($path, $extension){
    $contenido_file = "";
    //selector case que permite establecer la funcion que trabajara con el archivo
    switch($extension){
	case "doc":
	    $contenido_file = $this->lector_doc($path);
	break;
	case "docx":
	    $contenido_file = $this->lector_docx($path);
	break;
	case "odt":
	    $contenido_file = $this->lector_odt($path);
	break;
	case "ods":
	    $contenido_file = ""; //$var = $this->lector("ods");
	break;    
	case "xls":
	    $contenido_file = ""; //$var = $this->lector("xls");
	break;
	case "xlsx":
	    $contenido_file = ""; //$var = $this->lector("xlsx");
	break;
	case "pdf":
	    $contenido_file = ""; //$var = $this->lector("pdf");
	break;
	default:
	    $contenido_file = "Undefined File Type";
    }
    
    return @$contenido_file;

}



public function odt2text($filename) {
    return $this->readZippedXML($filename, "content.xml");
}

function docx2text($filename) {
    return $this->readZippedXML($filename, "word/document.xml");
}

public function readZippedXML($archiveFile, $dataFile) {

//require_once('pclzip.lib.php');$ListaArchivos = "texto1.txt,texto2.txt,texto3.txt,texto4.txt";  
//$archivo_zip = new PclZip('Nombre_Fichero.zip');  

    // Create new ZIP archive
    $zip = new ZipArchive;

    // Open received archive file
    if (true === $zip->open($archiveFile)) {
        // If done, search for the data file in the archive
        if (($index = $zip->locateName($dataFile)) !== false) {
            // If found, read it to the string
            $data = $zip->getFromIndex($index);
            // Close archive file
            $zip->close();
            // Load XML from a string
            // Skip errors and warnings
            $xml = DOMDocument::loadXML($data, LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING);
            // Return data without XML formatting tags
            return strip_tags($xml->saveXML());
        }
        $zip->close();
    }
	else {
    // In case of failure return empty string
    return "Res:".""; }
} 


function doc2text($userDoc) 
{
    $fileHandle = fopen($userDoc, "r");
    $line = fread($fileHandle, filesize($userDoc));   
    //fclose($fileHandle);

    $lines = explode(chr(0x0D),$line);

    $outtext = "";
    $posi = 0;

//echo $line."<p>COMPLETO<p>";
//echo $lines['0']."<p>0<p>";
//echo $lines['1']."<p>1<p>";
//echo $lines['2']."<p>2<p>";
//echo $lines['3']."<p>3<p>";
//echo $lines['4']."<p>4<p>";
//echo $lines['5']."<p>5<p>";
//echo $lines['6']."<p>6<p>";
//echo $lines['7']."<p>7<p>";
//echo $lines['8']."<p>8<p>";

//echo $lines['1'];

$outtext = $line;

    foreach($lines as $thisline)
      {
        $pos = strpos($thisline, chr(0x00));
	//echo "Valor POS: ".$pos."<br>";
	//echo "strlen: ".strlen($thisline)."<br>";

        if (($pos !== FALSE)||(strlen($thisline)==0))
          {
            //$outtext .= $thisline."<p>ccc";
	    //echo "agregado: ".$posi."*";
          } else {
            //$outtext .= $thisline."<p>cccx";
            //echo "agregado: ".$posi."-";
	    //echo $outtext."p:".chr(0x00);
          }
        $posi++;    
       //echo $posi;
      }

//$outtext = "askdjaksljdklasjkdasjkdjlasjjdlkjlaskjlda asdjaskljdklasjkl";
//$outtext .= "Élvarox";

/*
	$outtext = str_replace(utf8_decode("Á"),'Aacute;',$outtext);//NO RECONOCE LAS LETRAS CON ACENTO
	$outtext = str_replace(utf8_decode("É"),'Aacute;',$outtext);
	$outtext = str_replace(utf8_decode("Í"),'Aacute;',$outtext);
	$outtext = str_replace(utf8_decode("Ó"),'Aacute;',$outtext);
	$outtext = str_replace(utf8_decode("Ú"),'Aacute;',$outtext);
	$outtext = str_replace(utf8_decode("á"),'Aacute;',$outtext);
	$outtext = str_replace(utf8_decode("é"),'Aacute;',$outtext);
	$outtext = str_replace(utf8_decode("í"),'Aacute;',$outtext);
	$outtext = str_replace(utf8_decode("ó"),'Aacute;',$outtext);
	$outtext = str_replace(utf8_decode("ú"),'Aacute;',$outtext);
	$outtext = str_replace(utf8_decode("Ñ"),'Aacute;',$outtext);
	$outtext = str_replace(utf8_decode("ñ"),'Aacute;',$outtext);
*/

	//$outtext = str_replace("o","2",$outtext);

//$f=fopen("archivo.txt","w+");
//fwrite($f, $outtext . PHP_EOL); 
//fclose($f); 

     //$outtext = preg_replace("/[^a-zA-Z0-9\s\,\.\-\n\r\t@\/\_\(\)]/","",$outtext);
     //$outtext = preg_replace("/[^a-zA-Z0-9\áéíóúÁÉÍÓÚnÑ\s\,\.\-\n\r\t@\/\_\(\)]/","",$outtext);

	//echo $outtext."ggggg";

	//$outtext = htmlentities($outtext);

	//$outtext = preg_replace("/[^a-zA-Z0-9áéíóúÁÉÍÓÚnÑ\s\,\.\;\-\n\r\t@\/\_\(\)]/","",utf8_encode($outtext));

	$especiales = utf8_decode("áéíóúÁÉÍÓÚñÑ");
	$outtext = preg_replace("/[^a-zA-Z0-9\s\,\.\;\-\n\r\t@\/\_\(\)]/","",$outtext);

	//$outtext = preg_replace("/[^a-zA-Z0-9\s\,\.\-\n\r\t@\/\_\(\)]/","",$outtext);

	

	//$outtext = preg_replace("/[^A-Za-zÑñáéíóúÁÉÍÓÚ]$/","",$outtext);

    	//return utf8_decode($outtext);

    	return $outtext;

    //return htmlspecialchars($outtext);
} 


	function lector_odt($document)
	{
		//echo $document;
		return utf8_decode($this->odt2text($document));
	}

	function lector_docx($document)
	{
		//echo $document;
		return utf8_decode($this->docx2text($document));
	}

	function lector_doc($document)
	{
		//echo $document;
		return utf8_decode($this->doc2text($document));
	}

	function lector_ods($document)
	{
		//echo $document;
		return utf8_decode($this->doc2text($document));
	}


	function lector_xls($document) {

	error_reporting(E_ALL);
	//ini_set('include_path', ini_get('include_path').';./Classes/'); //Classes dir

	set_include_path(get_include_path() . PATH_SEPARATOR . './Classes/');

	require_once('Classes/PHPExcel.php');
	require_once('Classes/PHPExcel/Reader/Excel2007.php');

	//include 'PHPExcel/IOFactory.php';

		// Cargando la hoja de cálculo
		$objReader = new PHPExcel_Reader_Excel2007();
		$objPHPExcel = $objReader->load($document);

		$objPHPExcel->setActiveSheetIndex(0);
		$val = ($objPHPExcel->getActiveSheet()->getCell('A2'));
		$temp = $val->getvalue();//returns the value in the cell

	return $temp;
	
		// Asignar hoja de calculo activa
		//$objPHPExcel->setActiveSheetIndex(0);
	
		// Asignar data
		//$objPHPExcel->getActiveSheet()->setCellValue('automatico', $_REQUEST['transmision_Automatica']);


	//$objReader = PHPExcel_IOFactory::createReader('Excel5');//use excel2007 for 2007 format
	//$objPHPExcel = $objReader->load("c:\profile.xls");
	}

}

?>
